﻿@inject IHttpService _httpService
@inject IPopupService PopupService

@if (Model != null)
{
    <MDCard>
        <MCardTitle>
            @(Model.Id == 0 ? $"添加{_name}" : $"编辑{_name}")  @Model.Name
        </MCardTitle>
        <MCardText>
            <MRow>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.Modifier" Label="分组" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.PositionOfficial" Label="★官方职位" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.Name" Label="★唯一名称" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <CnGalWebSite.Components.Inputs.MasaSelecte @bind-Value="Model.PositionGeneral" TValue="PositionGeneralType" Label="通用职位" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.SubordinateOrganization" Label="隶属组织" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.CustomName" Label="自定义名称" />
                </MCol>
            </MRow>

        </MCardText>
        <MCardActions Class="pb-6 ps-6 pe-6">
            <MSpacer></MSpacer>
            <MButton Color="primary" Rounded OnClick="SaveEdit">
                 <MIcon Left>mdi-check</MIcon>
             保存
         </MButton>
     </MCardActions>

 </MDCard>
}




@code {
    [Parameter]
    public List<StaffModel> Staffs { get; set; }
    [Parameter]
    public EventCallback OnEdited { get; set; }
    [Parameter]
    public StaffModel Model { get; set; }

    private string _name = "Staff";


    public async Task SaveEdit()
    {
        var result = Model.Validate(Staffs);

        if (result.Successful == false)
        {
            await PopupService.ToastAsync(result.Error, AlertTypes.Error);
            return;
        }

        var flag = false;
        if (Model.Id == 0)
        {
            flag = true;
            if (Staffs.Count == 0)
            {
                Model.Id = 1;
            }
            else
            {
                Model.Id = Staffs.Max(i => i.Id) + 1;
            }
        }
        if (flag)
        {
            Staffs.Add(Model);
        }
        await OnEdited.InvokeAsync();

    }
}
